/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32g4xx_ll_adc.h"
#include "stm32g4xx_ll_cordic.h"
#include "stm32g4xx_ll_crc.h"
#include "stm32g4xx_ll_dma.h"
#include "stm32g4xx_ll_i2c.h"
#include "stm32g4xx_ll_opamp.h"
#include "stm32g4xx_ll_rcc.h"
#include "stm32g4xx_ll_bus.h"
#include "stm32g4xx_ll_crs.h"
#include "stm32g4xx_ll_system.h"
#include "stm32g4xx_ll_exti.h"
#include "stm32g4xx_ll_cortex.h"
#include "stm32g4xx_ll_utils.h"
#include "stm32g4xx_ll_pwr.h"
#include "stm32g4xx_ll_spi.h"
#include "stm32g4xx_ll_tim.h"
#include "stm32g4xx_ll_usart.h"
#include "stm32g4xx_ll_gpio.h"

#if defined(USE_FULL_ASSERT)
#include "stm32_assert.h"
#endif /* USE_FULL_ASSERT */

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdbool.h>
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
#define LED1_ON()  LL_GPIO_SetOutputPin(LED1_GPIO_Port, LED1_Pin);
#define LED1_OFF() LL_GPIO_ResetOutputPin(LED1_GPIO_Port, LED1_Pin);
#define LED1_TOGGLE() LL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);

#define LED2_ON()  LL_GPIO_SetOutputPin(LED2_GPIO_Port, LED2_Pin);
#define LED2_OFF() LL_GPIO_ResetOutputPin(LED2_GPIO_Port, LED2_Pin);
#define LED2_TOGGLE() LL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);

/* 
  将FOC相关的函数，放置在CCMRAM可以显著提升性能，参考ST的应用指南AN4296 
  需要同时修改*.ld文件和startup*.s文件
  The CCM SRAM is typically used for real-time and computation intensive routines, like the following:
  Digital power conversion control loops (switch-mode power supplies, lighting)
  Field-oriented 3-phase motor control
  Real-time DSP (digital signal processing) tasks

  不要同时存放函数和变量，会造成性能下降
  When the CCM SRAM is not used for code, it can hold data like an extra SRAM memory. It is not recommended
  to place both code and data together in the CCM, since the Cortex® core must then fetch code and data from the
  same memory with the risk of collisions. The core is then in the Von Neumann configuration and its performance
  drops from 1.25 DMIPS/MHz to below 1 DMIPS/MHz.
*/
#define CCMRAM __attribute__((section(".ccmram")))


/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* USER CODE BEGIN EFP */
/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define PWM_MAX 2500
#define LED1_Pin LL_GPIO_PIN_0
#define LED1_GPIO_Port GPIOC
#define LED2_Pin LL_GPIO_PIN_1
#define LED2_GPIO_Port GPIOC
#define WAKE_Pin LL_GPIO_PIN_7
#define WAKE_GPIO_Port GPIOE
#define READY_Pin LL_GPIO_PIN_14
#define READY_GPIO_Port GPIOE
#define NFAULT_Pin LL_GPIO_PIN_15
#define NFAULT_GPIO_Port GPIOE
#define SPI_CS_Pin LL_GPIO_PIN_15
#define SPI_CS_GPIO_Port GPIOA
#define CAL_EN_Pin LL_GPIO_PIN_2
#define CAL_EN_GPIO_Port GPIOD
#ifndef NVIC_PRIORITYGROUP_0
#define NVIC_PRIORITYGROUP_0         ((uint32_t)0x00000007) /*!< 0 bit  for pre-emption priority,
                                                                 4 bits for subpriority */
#define NVIC_PRIORITYGROUP_1         ((uint32_t)0x00000006) /*!< 1 bit  for pre-emption priority,
                                                                 3 bits for subpriority */
#define NVIC_PRIORITYGROUP_2         ((uint32_t)0x00000005) /*!< 2 bits for pre-emption priority,
                                                                 2 bits for subpriority */
#define NVIC_PRIORITYGROUP_3         ((uint32_t)0x00000004) /*!< 3 bits for pre-emption priority,
                                                                 1 bit  for subpriority */
#define NVIC_PRIORITYGROUP_4         ((uint32_t)0x00000003) /*!< 4 bits for pre-emption priority,
                                                                 0 bit  for subpriority */
#endif

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */
